package com.qinge.blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qinge.blog.dao.BlogTypeDao;
import com.qinge.blog.entity.BlogType;
import com.qinge.blog.entity.BlogTypeExample;
import com.qinge.blog.service.BlogContentTypeService;
import com.qinge.blog.utils.BeanMapper;
import com.qinge.blog.vo.BlogTypeVO;
import com.qinge.blog.vo.paging.PagingReqVO;
import org.springframework.stereotype.Service;
import com.qinge.blog.service.BlogTypeService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author QDW
 * @date 2022-05-02 23:38:51
 * @description BlogType service层实现类
 */
@Service
public class BlogTypeServiceImpl   implements BlogTypeService {
    @Resource
    BlogTypeDao typeDao;

    @Resource
    BlogContentTypeService contentTypeService;


    @Override
    public void add(BlogType... blogTypes) {
        typeDao.batchInsert(Arrays.asList(blogTypes));
    }

    @Override
    public void addByList(List<BlogType> blogTypes) {
        typeDao.batchInsert(blogTypes);
    }

    @Override
    public void removeById(String id) {
        typeDao.deleteByPrimaryKey(id);
    }

    @Override
    public void removeBatchByIds(List<String> ids) {
        BlogTypeExample example=new BlogTypeExample();
        BlogTypeExample.Criteria criteria=example.createCriteria();

        criteria.andIdIn(ids);

        typeDao.deleteByExample(example);
    }

    @Override
    public void update(BlogType blogType) {
        typeDao.upsert(blogType);
    }

    @Override
    public void upsert(BlogType blogType) {
        typeDao.upsert(blogType);
    }

    @Override
    public BlogType findById(String id) {
        return typeDao.selectByPrimaryKey(id);
    }

    @Override
    public List<BlogType> findByIds(List<String> ids) {
        BlogTypeExample example=new BlogTypeExample();
        BlogTypeExample.Criteria criteria=example.createCriteria();

        criteria.andIdIn(ids);

        return typeDao.selectByExample(example);
    }

    @Override
    public List<BlogType> findAll() {
        BlogTypeExample example=new BlogTypeExample();

        List<BlogType> list=typeDao.selectByExample(example);

        return list;
    }

    @Override
    public PageInfo<BlogTypeVO> findAll(PagingReqVO<BlogTypeVO> pagingReqVO) {
        return paging(pagingReqVO);
    }

    @Override
    public PageInfo<BlogTypeVO> paging(PagingReqVO<BlogTypeVO> pagingReqVO) {
        PageHelper.startPage(pagingReqVO);

        BlogTypeExample example=new BlogTypeExample();

        List<BlogType> list=typeDao.selectByExample(example);

        List<BlogTypeVO>  typeVOS= BeanMapper.mapList(list,BlogTypeVO.class);

        typeVOS.forEach(e->e.setCount(contentTypeService.findCountByTypeId(e.getId())));

        return new PageInfo<>(typeVOS);
    }

}

